package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Model1.orderDetail;

public class orderDetailDao {
	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;

	public static Boolean Insert(orderDetail detail) {
		if (detail == null)
			return false;
		conn = DatabaseConn.getConn();
		String sql = "insert into orderdetail(id,sales,orderid,price,name) values(?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, detail.getId());
			ps.setInt(2, detail.getSales());
			ps.setString(3, detail.getOrderId());
			ps.setDouble(4, detail.getPrice());
			ps.setString(5, detail.getName());
			int i = ps.executeUpdate();
			DatabaseConn.close(conn, ps, rs);
			return i == 1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static List<orderDetail> SearchByOrderId(String orderid) {
		if (orderid.length() < 1)
			return null;
		conn = DatabaseConn.getConn();
		List<orderDetail> detail = new ArrayList<>();
		String sql = "select * from orderdetail where orderid like '" + orderid + "'";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				detail.add(new orderDetail(rs.getInt("id"), rs.getString("orderid"), rs.getInt("sales"),
						rs.getFloat("price"), rs.getString("name")));
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return detail;
	}

	public static Boolean Delete(String orderid, int id) {
		if (orderid.length() <= 1)
			return false;
		if (id <= 0)
			return false;
		conn = DatabaseConn.getConn();
		String sql = "delete from orderdetail where orderid like " + orderid + "and id like" + id;
		try {
			ps = conn.prepareStatement(sql);
			int i = ps.executeUpdate();
			DatabaseConn.close(conn, ps, rs);
			return i == 1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static Boolean Update(orderDetail detail) {
		conn = DatabaseConn.getConn();
		String sql = "update orderdetail set sales=?,price=? where orderid=? and id=?";
		try {
			ps = conn.prepareStatement(sql);

			ps.setInt(1, detail.getSales());
			ps.setDouble(2, detail.getPrice());
			ps.setString(3, detail.getOrderId());
			ps.setInt(4, detail.getId());
			int i = ps.executeUpdate();
			DatabaseConn.close(conn, ps, rs);
			return i == 1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	

	public static double SearchByName(String name) {
		if (name.length() < 1)
			return 0;
		double d=0;
		conn = DatabaseConn.getConn();
		List<orderDetail> detail = new ArrayList<>();
		String sql = "select sales from orderdetail where name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				d+=rs.getDouble("sales");
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return d;
	}

	public static List<orderDetail> findAll() {
		conn = DatabaseConn.getConn();
		List<orderDetail> detail = new ArrayList<>();
		String sql = "select * from orderdetail";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				detail.add(new orderDetail(rs.getInt("id"), rs.getString("orderid"), rs.getInt("sales"),
						rs.getFloat("price")));
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return detail;
	}
}